package com.zx.mq.topic;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicConfigure {

    @Bean
    public Queue topicQueueA() {
        return new Queue("topic.A");
    }

    @Bean
    public Queue topicQueueB() {
        return new Queue("topic.B");
    }

    // 交换机
    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }

    // 路由模式，绑定队列到交换机上,需要完整匹配topic.message，才能接受
    @Bean
    Binding bindingExchangeRouter(Queue topicQueueA, TopicExchange exchange) {
        return BindingBuilder.bind(topicQueueA).to(exchange).with("topic.00");
    }

    // 通过路由规则，绑定队列到交换机上。所有以topic开头的routingKey都输入到QueueB
    // 符号#匹配一个或多个词，符号*匹配不多不少一个词(单词以点号隔开)
    @Bean
    Binding bindingExchangeTopic(Queue topicQueueB, TopicExchange exchange) {
        return BindingBuilder.bind(topicQueueB).to(exchange).with("topic.#");
    }
}
